﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using YKXiangQi.Controls;
using YKXiangQi.ViewModels;

namespace YKXiangQi.Utilities
{
    public class DialogService: IDialogService
    {
        public bool? ShowDialog(string title, object dataContext, string icon)
        {
            var viewModel = dataContext as DialogViewModel;
            if (viewModel == null)
                return null;

            var dialog = new Dialog();
            dialog.Title = title;
            dialog.DataContext = dataContext;
            if (!string.IsNullOrEmpty(icon))
            {
                string path = string.Format(@"pack://application:,,,/YKXiangQi;component/Images/{0}", icon);
                dialog.Icon = new BitmapImage(new Uri(path));
            }
            dialog.Owner = System.Windows.Application.Current.MainWindow;
            viewModel.RequestClose += (s, e) =>
                                          {
                                              dialog.DialogResult = viewModel.DialogResult;
                                              dialog.Close();
                                          };

            
            return dialog.ShowDialog();
        }

        public bool? ShowDialog(object dataContext)
        {
            return ShowDialog("YKXiangQi", dataContext, string.Empty);
        }

        public bool? ShowDialog(string title, object dataContext)
        {
            return ShowDialog(title, dataContext, string.Empty);
        }

        public string BrowseForFile(string filter)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "Select a file...";
            fileDialog.Filter = filter;

            var dialogResult = fileDialog.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
                return fileDialog.FileName;

            return string.Empty;
        }

        public void ShowMessageBox(string message)
        {
            MessageBox.Show(message);
        }
    }
}
